import { Dispatcher } from "./dispatcher";
import { Mediator } from "./mediator";
import { AppEvent } from "./app_event";
import { Router } from "./router";
import { Route } from "./route";

class App {
  private _dispatcher: IDispatcher;
  private _mediator: IMediator;
  private _router: IRouter;
  private _controllers: IControllerDetails[];
  private _onErrorHandler: (o: Object) => void;

  constructor(appSettings: IAppSettings) {
    this._controllers = appSettings.controllers;
    this._mediator = new Mediator(appSettings.isDebug || false);
    this._router = new Router(this._mediator, appSettings.defaultController, appSettings.defaultAction);
    this._dispatcher = new Dispatcher(this._mediator, this._controllers);
    this._onErrorHandler = appSettings.onErrorHandler;
  }

  public initialize() {
    this._router.initialize();
    this._dispatcher.initialize();
    this._mediator.subscribe(new AppEvent("app.error", null, (e: any, data?: any) => {
      this._onErrorHandler(data);
    }));
    // 改变初始化流程
    // this._mediator.publish(new AppEvent("app.initialize", null, null));
  }

  public triggerRouteChange(url: any);
  public triggerRouteChange(url: any, data: any);
  public triggerRouteChange(url: any, data?: any) {
    this._mediator.publish(new AppEvent("app.initialize", { url: url, data: data }, null));
  }

  public onSendJson(callback: (topic: string, json: string) => void) {
    this._mediator.subscribe(new AppEvent("app.sendjson", null, callback));
  }

  public onSendError(callback: (topic: string, error: string) => void) {
    this._mediator.subscribe(new AppEvent("app.error", null, callback));
  }

}

export { App };
